package com.zara.store.client.clientmodel;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import javax.naming.NamingException;

import com.thoughtworks.xstream.XStream;
import com.zara.store.client.model.StoreProxy;
import com.zara.store.common.EnvtVO;
import com.zara.store.common.ProductVO;

import framework.clientmodel.Model;

public class EnvtModel extends Model {
	
	private StoreProxy sp;
	private EnvtVO envtVO;
	
	public EnvtModel(StoreProxy sp) throws NamingException {
		this.sp = sp;
	}
	
	public void createEnvt(File file) throws IOException {
		XStream xstream = new XStream();

		xstream.addDefaultImplementation(ArrayList.class, Collection.class);
//		xstream.addDefaultImplementation(java.sql.Date.class, java.util.Date.class);
				
		xstream.alias("envt", EnvtVO.class);
		xstream.alias("product", ProductVO.class);
		
		FileInputStream is = null;
		try {
			is = new FileInputStream(file);
			EnvtVO envtVO1 = (EnvtVO)xstream.fromXML(is);
			envtVO = sp.createEnvt(envtVO1);
		} finally {
			if(is != null) {
				is.close();
			}
		}

		notifyViews();
	}
	
	public Collection<ProductVO> getItems() {
		return envtVO.getItems();
	}

	public Collection<ProductVO> getPendingItems() {
		return envtVO.getPendingItems();
	}

	public boolean hasEnvt() {
		return envtVO != null;
	}

}
